<template>
  <q-page padding>
    <!-- content -->
    <div class="auth-panel">
      <div class="auth-line auth-title">注册账户</div>
      <div class="auth-hint">
        已有账户？
        <router-link class="auth-signup-button" to="/auth/signin"
          >立即登陆</router-link
        >
      </div>

      <el-form ref="signUpFormRef" :model="signUpForm" :rules="signUpFormRules">
        <auth-error-message :error="formErrorsRef?.username" />
        <el-form-item prop="username">
          <el-input v-model="signUpForm.username" placeholder="用户" />
        </el-form-item>
        <auth-error-message :error="formErrorsRef?.password" />
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="signUpForm.password"
            placeholder="密码"
          />
        </el-form-item>
        <auth-error-message :error="formErrorsRef?.password_confirm" />
        <el-form-item prop="password_confirm">
          <el-input
            style="width: 100%"
            type="password"
            v-model="signUpForm.password_confirm"
            placeholder="确认密码"
          />
        </el-form-item>
        <!--
        <el-input
          class="auth-line"
          type="tel"
          v-model="phone"
          placeholder="手机"
        />
        <div class="auth-line auth-code-line">
          <el-input
            class="code-input"
            type="text"
            v-model="code"
            placeholder="短信验证码"
          />
          <el-button class="send-code">获取验证码</el-button>
        </div>
        -->
        <auth-error-message :error="formErrorsRef?.non_field_errors" />
        <el-form-item>
          <el-button
            style="width: 100%"
            type="primary"
            @click="submitSignUpForm(signUpFormRef)"
            >注 册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </q-page>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { signUp, SignUpErrorResponse, SignUpErrors } from 'src/api/common';
import AuthErrorMessage from 'components/AuthErrorMessage.vue';
import type { FormInstance } from 'element-plus';
import { useRouter } from 'vue-router';

const router = useRouter();

const signUpFormRef = ref<FormInstance>();

const signUpForm = reactive({
  username: '',
  password: '',
  password_confirm: '',
});

const formErrorsRef = ref<SignUpErrors>();

const signUpFormRules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'change' },
    { min: 6, message: '长度必须大于6', trigger: 'blur' },
  ],
  password: [
    {
      required: true,
      message: '请输入密码',
      trigger: 'change',
    },
    { min: 8, message: '长度必须大于8', trigger: 'blur' },
  ],
  password_confirm: [
    {
      required: true,
      message: '请确认密码',
      trigger: 'change',
    },
    { min: 8, message: '长度必须大于8', trigger: 'blur' },
  ],
});

const submitSignUpForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid) => {
    if (valid) {
      signup();
    }
  });
};

function signup() {
  formErrorsRef.value = {};
  signUp({
    username: signUpForm.username,
    password: signUpForm.password,
    password_confirm: signUpForm.password_confirm,
  })
    .then(() => {
      router.replace('/user/signup/success');
    })
    .catch((error: SignUpErrorResponse) => {
      const type = error?.response?.data.type;
      if (type == 'invalid_request') {
        formErrorsRef.value = error.response?.data.errors;
      } else if (type == 'username_exists') {
        const title = error?.response?.data.title;
        if (title) {
          formErrorsRef.value = {
            non_field_errors: [title],
          };
        }
      }
    });
}
</script>
